import re
import requests
import pymysql
from pandas import DataFrame


class JokeSpider():
    # 初始化
    def __init__(self):
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
        }
        self.url = "https://www.bqgda.cc/"

    # 发送网络请求

    def send_request(self, url):
        response = requests.get(url)
        return response

    # 解析数据
    def parse_content(self,response):
        content = response.text
        new_content = "".join(re.findall('<div.*?class="l bd">(.*?)</div>', content, re.S))
        titles = re.findall("<li>.*?<a.*?>(.*?)</a>", new_content, re.S)
        if titles:
            self.save_content(titles)

    # 保存数据
    def save_content(self,content):
        df = DataFrame({"titles": content})
        df.to_excel("titles.xlsx", index=False)

    # 开启爬虫
    def start(self):
        response = self.send_request(self.url)
        if response.status_code == 200:
            self.parse_content(response)


if __name__ == '__main__':
    js = JokeSpider()
    js.start()
